package com.zhuxulin.tab.threads;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.util.Log;

import com.zhuxulin.midi.MidiFile;
import com.zhuxulin.midi.event.ChannelEvent;
import com.zhuxulin.midi.event.MidiEvent;
import com.zhuxulin.midi.util.MidiEventListener;
import com.zhuxulin.midi.util.MidiProcessor;
import com.zhuxulin.tab.App;
import com.zhuxulin.tab.bluetooth.ISendMsg;
import com.zhuxulin.tab.utils.LogUtils;

public class PlayMidOnPiano extends Thread implements Runnable {

	private static final String TAG = PlayMidOnPiano.class.getSimpleName();
	private ISendMsg sendMsg = null;
	private MidiProcessor processor = null;
	private App app=null;
	private File midiFile=null;
	private boolean isStarted=false;
	private MidiEventListener listener=new MidiEventListener() {
//		ISendMsg sendMsg = app.getSendMsg();

		@Override
		public void onStop(boolean finished) {
			LogUtils.i(TAG, "onStop!Finished:"+finished);
		}

		@Override
		public void onStart(boolean fromBeginning) {
			LogUtils.i(TAG, "onStart!FromeBeginning:"+fromBeginning);
		}

		@Override
		public void onEvent(MidiEvent event, long ms) {
			if (event == null) {
				LogUtils.i(TAG, "event is null!");
				// processor.stop();
				return;
			}
			if (event instanceof ChannelEvent) {
				if (event.getCmd() == null) {
					LogUtils.i(TAG, "cmd is null!");
					// processor.stop();
					return;
				}
				if ((sendMsg = app.getSendMsg()) == null) {
					LogUtils.i(TAG, "SendMsg is null,plese connect it!");
					LogUtils.i(TAG, "event:"+event);
//					LogUtils.i(TAG, "event:"+event.getCmd());
					processor.stop();
					return;
				}
				LogUtils.i(TAG, "BeginSendCmd");
				sendMsg.sendMessage(event.getCmd());
				LogUtils.i(TAG, "EndSendCmd");
			}
		}

		@Override
		public void onEvents(ArrayList<MidiEvent> events, long mMsElapsed) {
			// TODO Auto-generated method stub
			
		}
	};
	public PlayMidOnPiano(ISendMsg sendMsg) {
		this.sendMsg = sendMsg;
	}
	public PlayMidOnPiano(App app,String fileName){
		this(app,new File(fileName));
	}
	public PlayMidOnPiano(App app){
		this.app=app;
		this.processor=app.getProcessor();
//		this(app,null);
	}
	public PlayMidOnPiano(App app,File midiFile){
		this.app=app;
//		this.sendMsg=app.getSendMsg();
		this.midiFile=midiFile;
		this.processor=app.getProcessor();
	}
	public void setFile(File midiFile){
		this.midiFile=midiFile;
	}
	@Override
	public void run() {
		if (processor == null) {
			try {
				processor = new MidiProcessor(new MidiFile(midiFile));
				app.setProcessor(processor);
				// processor = new MidiProcessor(new MidiFile(new File(
				// "/mnt/sdcard/Music/1.mid")));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			processor.registerEventListener(listener, MidiEvent.class);
			LogUtils.i(TAG, "Starting thread...");
			processor.start();
			LogUtils.i(TAG, "End thread...");
		} else if (processor.isStarted() || processor.isRunning()) {
			processor.stop();
			try {
				processor.setMidiFile(new MidiFile(midiFile));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			processor.registerEventListener(listener, MidiEvent.class);
			LogUtils.i(TAG, "Starting thread...");
			processor.start();
			LogUtils.i(TAG, "End thread...");
		}
	}
}
